/*
 * Ext JS Library 2.0 Alpha 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */


// Very simple plugin for adding a close context menu to tabs

Ext.ux.TabCloseMenu = function(config){
    Ext.apply(this, config);
};

Ext.extend(Ext.ux.TabCloseMenu, Ext.util.Observable, {
    init: function(tp){
        this.tabs = tp;
        this.tabs.on('contextmenu', this.onContextMenu);
    },
    
    onContextMenu:function (ts, item, e){
        if(!this.menu){ // create context menu on first right click
            this.menu = new Ext.menu.Menu([{
                id: ts.id + '-close',
                text: 'Close Tab',
                handler : function(){
                    ts.remove(ctxItem);
                }
            },{
                id: ts.id + '-close-others',
                text: 'Close Other Tabs',
                handler : function(){
                    ts.items.each(function(item){
                        if(item.closable && item != ctxItem){
                            ts.remove(item);
                        }
                    });
                }
            }]);
        }
        ctxItem = item;
        var items = this.menu.items;
        items.get(ts.id + '-close').setDisabled(!item.closable);
        var disableOthers = true;
        ts.items.each(function(){
            if(this != item && this.closable){
                disableOthers = false;
                return false;
            }
        });
        items.get(ts.id + '-close-others').setDisabled(disableOthers);
        this.menu.showAt(e.getPoint());
    }
});